#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <poll.h>


int main(int argc, char *argv[])
{
    int fd,ret;
    int key_val;
    fd_set readfds;

    fd=open(argv[1], O_RDWR | O_NONBLOCK);
    if(fd<0){
        printf("ERROR: file %s open failed!\n", argv[1]);
        return -1;
    }

    FD_ZERO(&readfds);
    FD_SET(fd, &readfds);

    for(;;){
        ret=select(fd+1, &readfds, NULL, NULL, NULL);
        switch(ret){
            case 0:
                printf("Poll timeout!\n");
                break;
            case -1:
                perror("Poll failed!\n");
                break;
            default:
                if(FD_ISSET(fd, &readfds)){
                    read(fd, &key_val, sizeof(int));
                    if(key_val==0x0){
                        printf("Key Pressed!\n");
                    }
                    else if(key_val==0x1){
                        printf("Key Released!\n");
                    }
                }
                break;
        }

    }

    close(fd);
    return 0;
}